﻿namespace FellowshipOne.API.Base {

    public interface IRestClient
    {
        Realms.IPersonRealm PeopleRealm { get; set; }
        Realms.GivingRealm GivingRealm { get; set; }
        Realms.IGroupsRealm GroupsRealm { get; set; }
        Realms.IEventsRealm EventsRealm { get; set; }
    }

    public class RestClient : Restify.Client, IRestClient {

        #region Properties
        F1OAuthTicket _ticket { get; set; }
        bool _useDemo = false;

        #region Api Sets
        public Realms.IPersonRealm PeopleRealm { get; set; }
        public Realms.GivingRealm GivingRealm { get; set; }
        public Realms.IGroupsRealm GroupsRealm { get; set; }
        public Realms.IEventsRealm EventsRealm { get; set; }
        #endregion Api Sets

        #endregion Properties

        #region Constructor
        public RestClient(F1OAuthTicket ticket, bool isStaging = false, bool useDemo = false)
            : base(ticket) {
            var baseUrl = isStaging ? $"https://{ticket.ChurchCode}.staging.fellowshiponeapi.com/"
                : $"https://{ticket.ChurchCode}.fellowshiponeapi.com/";
            
            SetProperties(ticket, useDemo, baseUrl);
        }
        public RestClient(F1OAuthTicket ticket, string baseUrl, bool isSecure = false, bool useDemo = false)
            : base(ticket) {
            var url = $"{(isSecure == true ? "https" : "http")}://{ticket.ChurchCode}.{baseUrl}/";
            SetProperties(ticket, useDemo, url);
        }
        private void SetProperties(F1OAuthTicket ticket, bool useDemo, string baseUrl) {
            _ticket = ticket;
            _useDemo = useDemo;
            PeopleRealm = new Realms.PersonRealm(ticket, baseUrl);
            GivingRealm = new Realms.GivingRealm(ticket, baseUrl);
            GroupsRealm = new Realms.GroupsRealm(ticket, baseUrl);
            EventsRealm = new Realms.EventsRealm(ticket, baseUrl);
        }

        #endregion Constructor

    }

    public enum LoginType {
        PortalUser = 1,
        WeblinkUser = 2
    }
}
